package routers

import (
	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"
	"net/http"
	"strings"
	"time"
	"wms/controller"
	_ "wms/docs"
	"wms/middleware"
)

func SetupRouter() *gin.Engine {
	//gin.SetMode(gin.ReleaseMode) // 设置发布模式
	//r := gin.New()
	//r.Use(logger.GinLogger(), logger.GinRecovery(true))
	r := gin.Default()

	r.LoadHTMLGlob("static/dist/*.html")
	r.Static("/assets", "./static/dist/assets")

	r.GET("/", func(c *gin.Context) {
		if strings.HasSuffix(c.Request.RequestURI, ".js") {
			c.Header("Content-Type", "application/javascript; charset=utf-8")
		} else {
			c.Header("Content-Type", "text/html; charset=utf-8")
		}
		c.HTML(http.StatusOK, "index.html", nil)
	})

	r.Use(middleware.Cors())
	// 限流：2s通过一个令牌
	r.GET("/ping", middleware.RateLimitMiddleware(2*time.Second, 1), func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})
	r.GET("/swagger/*any", gs.WrapHandler(swaggerfiles.Handler))

	// ------------------------      无需验证鉴权      ------------------------ //

	// 登录
	r.POST("/login", controller.LoginHandler)

	// ------------------------     需要验证 Token     ------------------------ //
	loginAuth := r.Group("/").Use(middleware.LoginAuthCheck())

	// 修改密码
	loginAuth.PUT("/settings/user/update/password", controller.UpdateUserPasswordHandler)
	// 修改头像
	loginAuth.PUT("/settings/user/update/avatar", controller.UpdateUserAvatarHandler)
	// 获取个人详情信息
	loginAuth.GET("/settings/user/get/detail", controller.GetUserDetailHandler)

	// ------------------------        需要鉴权        ------------------------ //
	funcAuth := loginAuth.Use(middleware.FuncAuthCheck())

	/// ----------------------- 菜单 ----------------------- ///

	// 菜单列表
	funcAuth.GET("/settings/menu/get/list", controller.GetMenuListHandler)
	// 新增菜单
	funcAuth.POST("/settings/menu/create", controller.CreateMenuHandler)
	// 更新菜单
	funcAuth.PUT("/settings/menu/update", controller.UpdateMenuHandler)
	// 删除菜单
	funcAuth.DELETE("/settings/menu/delete", controller.DeleteMenuHandler)

	/// ----------------------- 角色 ----------------------- ///
	// 角色列表
	funcAuth.GET("/settings/role/get/list", controller.GetRoleListHandler)
	// 获取角色详情
	funcAuth.GET("/settings/role/get/detail", controller.GetRoleDetailHandler)
	// 创建角色
	funcAuth.POST("/settings/role/create", controller.CreateRoleHandler)
	// 更新角色
	funcAuth.PUT("/settings/role/update", controller.UpdateRoleDetailHandler)
	// 更新角色管理员状态
	funcAuth.PUT("/settings/role/update/admin", controller.UpdateRoleAdminHandler)
	// 删除角色
	funcAuth.DELETE("/settings/role/delete", controller.DeleteRoleHandler)

	/// ----------------------- 用户 ----------------------- ///
	// 用户列表
	funcAuth.GET("/settings/user/get/list", controller.GetUserListHandler)
	// 创建用户
	funcAuth.POST("/settings/user/create", controller.CreateUserHandler)
	// 更新用户
	funcAuth.PUT("/settings/user/update", controller.UpdateUserHandler)
	// 删除用户
	funcAuth.DELETE("/settings/user/delete", controller.DeleteUserHandler)

	/// ----------------------- 功能 ----------------------- ///
	//功能列表
	funcAuth.GET("/settings/func/get/list", controller.GetFuncListHandler)

	/// ----------------------- 404 ----------------------- ///
	r.NoRoute(func(c *gin.Context) {
		if strings.HasSuffix(c.Request.RequestURI, ".js") {
			c.Header("Content-Type", "application/javascript; charset=utf-8")
		} else {
			c.Header("Content-Type", "text/html; charset=utf-8")
		}
		c.HTML(http.StatusOK, "index.html", nil)
	})
	return r
}
